package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Message;
public class UC3 extends AppCompatActivity {


    private int currentColor =0;
    final int [] colors = new int[]
    {
            R.color.color01,
            R.color.color02,
            R.color.color03,
            R.color.color04,
            R.color.color05,
    };
    final int[] names =new int[]{

            R.id.view1,
            R.id.view2,
            R.id.view3,
            R.id.view4,
            R.id.view5,
    };
    TextView[] views = new TextView[names.length];
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            //表明消息来自本程序
            System.out.println(msg.what);
            if(msg.what == 0x123) {
                for(int i = 0 ;i<names.length;i++) {
                    views[i].setBackgroundResource(colors[(i+currentColor)%names.length]);
                }
                currentColor++;
            }
            super.handleMessage(msg);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        for (int i = 0; i < names.length; i++) {
            views[i] = (TextView)findViewById(names[i]);

        }
        //定义一个线程周期性的改变currentColor变量的值
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                //发送一条空消息来通知系统改变颜色
                handler.sendEmptyMessage(0x123);
            }
        }, 0,1000);

    }



}
